package com.orangelife.mobile.individual.activity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.common.sp.UserSp;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;

public class NickNameChange2Activity extends OrangeLifeBaseActivity {

    /** 标题 */
    private TextView tvTitle, tvTitleRight;
    /** 昵称 */
    private EditText etNickName;
    /** 按钮 */
    private Button btnDone;


    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_change_nickname_2);
        initeView();
        findView();
    }

    /** 初始化控件 */
    private void initeView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitleRight = (TextView) findViewById(R.id.tv_title_right);
        etNickName = (EditText) findViewById(R.id.etNickName);
        btnDone = (Button) findViewById(R.id.btnDone);
    }

    /** 为控件赋值或设置状态等 */
    private void findView() {
        tvTitle.setText("修改昵称");
        if (StringUtil.isContainBlank(UserSp.getNickname())) {
            etNickName.setText("游客");
        } else {
            etNickName.setText(UserSp.getNickname());
        }
        tvTitleRight.setVisibility(View.GONE);
        btnDone.setOnClickListener(listener);

        etNickName.addTextChangedListener(mTextWatcher);
    }

    /** 监听器 */
    private OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if ("".equals(etNickName.getText().toString())) {
                ToastHelper.getInstance()._toast("昵称不能为空");
            } else {
                sendNewNickName();
            }

        }
    };


    /**
     * 处理服务器的返回结果
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            closeDialog();
            switch (msg.what) {
                case Constant.WHAT_ERROR_HTTP:
                    if ("2".equals(msg.obj)) {
                        ToastHelper.getInstance()._toast(String.valueOf("昵称不能为null"));
                    } else {
                        ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
                    }
                    break;
                case 4:
                    ToastHelper.getInstance().displayToastShort(
                            getResources().getString(R.string.modify_nickname));
                    UserSp.setNickname(etNickName.getText().toString());
                    finish();
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
            }
        }

    };

    /**
     * 修改昵称
     */
    private void sendNewNickName() {
        showDialog(this);
        Map<String, Object> map = new HashMap<String, Object>();
        String nickname = etNickName.getText().toString();
        map.put("memberID", UserSp.getLoginID());
        map.put("nickname", nickname);
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_MODIFY_NICKNAME);
        rootMap.put("wat", 4);
        new JSONRequest(JSONRequest.PUT, rootMap, handler, HttpUtil.ACTION_JSON);
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
//          mTextView.setText(s);//将输入的内容实时显示
        }

        @Override
        public void afterTextChanged(Editable s) {
            editStart = etNickName.getSelectionStart();
            editEnd = etNickName.getSelectionEnd();
//            mTextView.setText("您输入了" + temp.length() + "个字符");  
            if (temp.length() > 10) {
                ToastHelper.getInstance()._toast("昵称不能超过10个字符");
                s.delete(editStart - 1, editEnd);
                etNickName.setText(s);
            }
        }

    };

}
